package com.example.demo.rx;

import com.example.demo.rx.cat.ApiWrapperR;
import com.example.demo.rx.cat.Cat;
import io.reactivex.rxjava3.core.Observable;

import java.net.URL;
import java.util.Collections;
import java.util.List;

public class CatsHelper4Rx {
    ApiWrapperR apiWrapper;
    public Observable<URL> saveTheCutest(String query) {
//        List<Cat> cats = api.queryCat(query);
//        Cat cutest = findCutest(cats);
//        URL saveUrl = api.store(cutest);
//        return saveUrl;
        var catsListObservalble = apiWrapper.queryCats(query);
        var cutestCatObservalble = catsListObservalble.map(this::findCutest);
        var storeURLObservalble = cutestCatObservalble.flatMap(apiWrapper::storeCats);
        storeURLObservalble.blockingSingle();
        return storeURLObservalble;
    }

    private Cat findCutest(List<Cat> cats) {
        return Collections.max(cats);
    }
}
